package com.hp.attendance.dao;

import com.hp.attendance.entity.EmpRole;
import com.hp.attendance.entity.Employee;
import com.hp.attendance.entity.Menu;
import com.hp.attendance.entity.RoleMenu;
import com.hp.attendance.util.BaseDAO;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class EmployeeDao extends BaseDAO {

    /**
     * 根据员工编号和密码查询员工对象
     * @param employeeNumber
     * @param password
     * @return
     */
    public Employee getEmployee(String employeeNumber ,String password) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql="select * from employee where employee_number =? and password=?";
        Object[] obj = {employeeNumber,password};
        Employee employee = queryOne(sql, obj, new Employee());
        return employee;
    }

    /**
     * 分页查询记录
     * @param pageNo 当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    public List<Employee> getLimitList(int pageNo, int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select e.* from employee e " +
                " left join history h on e.employee_number=h.employee_number where h.status='在职' limit ?,?";
        Object[] obj ={(pageNo-1)*pageSize,pageSize};
        List<Employee> employees = queryList(sql, obj, new Employee());
        return employees;
    }

    /**
     * 查询总记录数
     * @return
     * @throws SQLException
     */
    public long getTotal() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select count(1) from employee";
        int count = queryCount(sql, new Object[0]);
        return count;
    }


    /**
     * 根据emp_id删除已分配的角色
     * @param empId
     */
    public void delEmpRole(int empId) throws SQLException, IOException, ClassNotFoundException {
        String sql ="delete from emp_role where emp_id=?";
        Object[] obj = {empId};
        addOrUpdOrDel(sql,obj);
    }

    /**
     * 执行角色分配功能
     * @param lister
     */
    public void addEmpRole(List<EmpRole> lister) throws SQLException, IOException, ClassNotFoundException {
        StringBuilder sql =new StringBuilder("insert into emp_role values");
        for (int i = 0; i < lister.size(); i++) {
            sql.append("(?,?),");
        }
        System.out.println(sql.toString());
        String sqlExe = sql.substring(0, sql.length() - 1);
        //设置参数
        Object[] obj=new Object[lister.size()*2];
        for (int i = 0; i < lister.size(); i++) {
            EmpRole empRole = lister.get(i);
            obj[2*i]=empRole.getEmp_id(); // 0    2   4  6
            obj[2*i+1]=empRole.getR_id();// 1   3   5  7
        }
        addOrUpdOrDel(sqlExe,obj);
    }

    /**
     * 查询员工全部信息
     * @return
     */
    public List<Employee> getList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from employee";
        List<Employee> list = queryList(sql, new Object[0], new Employee());
        return list;
    }

}
